home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / perl5 / Glib / MakeHelper.pm < prev    next >
Encoding:
Perl POD Document  |  2006-09-25  |  15.2 KB  |  538 lines

  1. #
  2. # $Header: /cvsroot/gtk2-perl/gtk2-perl-xs/Glib/MakeHelper.pm,v 1.36 2006/07/14 18:43:45 kaffeetisch Exp $
  3. #
  4.  
  5. package Glib::MakeHelper;
  6.  
  7. our $VERSION = '0.03';
  8.  
  9. =head1 NAME
  10.  
  11. Glib::MakeHelper - Makefile.PL utilities for Glib-based extensions
  12.  
  13. =head1 SYNOPSIS
  14.  
  15.  eval "use Glib::MakeHelper; 1"
  16.      or complain_that_glib_is_too_old_and_die();
  17.  
  18.  %xspod_files = Glib::MakeHelper->do_pod_files (@xs_files);
  19.  
  20.  package MY;
  21.  sub postamble {
  22.      return Glib::MakeHelper->postamble_clean ()
  23.           . Glib::MakeHelper->postamble_docs (@main::xs_files)
  24.           . Glib::MakeHelper->postamble_rpms (
  25.                  MYLIB     => $build_reqs{MyLib},
  26.             );
  27.  }
  28.  
  29. =head1 DESCRIPTION
  30.  
  31. The Makefile.PL for your typical Glib-based module is huge and hairy, thanks to
  32. all the crazy hoops you have to jump through to get things right.  This module
  33. wraps up some of the more intense and error-prone bits to reduce the amount of
  34. copied code and potential for errors.
  35.  
  36. =cut
  37.  
  38. use strict;
  39. use warnings;
  40. use Carp;
  41. use Cwd;
  42.  
  43. our @gend_pods = ();
  44.  
  45. =head1 METHODS
  46.  
  47. =over
  48.  
  49. =item HASH = Glib::MakeHelper->do_pod_files (@xs_files)
  50.  
  51. Scan the I<@xs_files> and return a hash describing the pod files that will
  52. be created.  This is in the format wanted by WriteMakefile(). If @ARGV contains
  53. the string C<disable-apidoc> an empty list will be returned and thus no apidoc
  54. pod will be generated speeding up the build process.
  55.  
  56. =cut
  57.  
  58. sub do_pod_files
  59. {
  60.     return () if (grep /disable[-_]apidoc/i, @ARGV);
  61.     print STDERR "Including ApiDoc pod...\n";
  62.  
  63.     shift; # package name
  64.  
  65.     # try to get it from pwd first, then fall back to installed
  66.     # this is so Glib will get associated copy, and everyone else
  67.     # should use the installed glib copy
  68.     eval { require 'ParseXSDoc.pm'; 1; } or require Glib::ParseXSDoc;
  69.     $@ = undef;
  70.     import Glib::ParseXSDoc;
  71.  
  72.     my %pod_files = ();
  73.  
  74.     open PARSE, '>build/doc.pl';
  75.     select PARSE;
  76.     my $pods = xsdocparse (@_);
  77.     select STDOUT;
  78.     @gend_pods = ();
  79.     foreach (@$pods)
  80.     {
  81.         my $pod = $_;
  82.         my $path = '$(INST_LIB)';
  83.         $pod = File::Spec->catfile ($path, split (/::/, $_)) . ".pod";
  84.         push @gend_pods, $pod;
  85.         $pod_files{$pod} = '$(INST_MAN3DIR)/'.$_.'.$(MAN3EXT)';
  86.     }
  87.     $pod_files{'$(INST_LIB)/$(FULLEXT)/index.pod'} = '$(INST_MAN3DIR)/$(NAME)::index.$(MAN3EXT)';
  88.  
  89.     return %pod_files;
  90. }
  91.  
  92. =item LIST = Glib::MakeHelper->select_files_by_version ($stem, $major, $minor)
  93.  
  94. Returns a list of all files that match "$stem-\d+\.\d+" and for which the first
  95. number is bigger than I<$major> and the second number is bigger than I<$minor>.
  96. If I<$minor> is odd, it will be incremented by one so that the version number of
  97. an upcoming stable release can be used during development as well.
  98.  
  99. =cut
  100.  
  101. sub select_files_by_version {
  102.     my ($class, $stem, $major, $minor) = @_;
  103.  
  104.     # make minors even, so that we don't have to deal with stable/unstable
  105.     # file naming changes.
  106.     $minor++ if ($minor % 2);
  107.  
  108.     my @files = ();
  109.     foreach (glob $stem . '-*.*') {
  110.         if (/$stem-(\d+)\.(\d+)/) {
  111.             push @files, $_
  112.                 if  $1 <= $major
  113.                 and $2 <= $minor;
  114.         }
  115.     }
  116.  
  117.     return @files;
  118. }
  119.  
  120. =item LIST = Glib::MakeHelper->read_source_list_file ($filename)
  121.  
  122. Reads I<$filename>, removes all comments (starting with "#") and leading and
  123. trailing whitespace, and returns a list of all lines that survived the treatment.
  124.  
  125. =cut
  126.  
  127. sub read_source_list_file {
  128.     my ($class, $filename) = @_;
  129.     my @list = ();
  130.     open IN, $filename or die "can't read $filename: $!\n";
  131.     while (<IN>) {
  132.         s/#.*$//;        # eat comments
  133.         s/^\s*//;        # trim leading space
  134.         s/\s*$//;        # trim trailing space
  135.         push @list, $_ if $_;    # keep non-blanks
  136.     }
  137.     close IN;
  138.     return @list;
  139. }
  140.  
  141. =item string = Glib::MakeHelper->postamble_clean (@files)
  142.  
  143. Create and return the text of a realclean rule that cleans up after much 
  144. of the autogeneration performed by Glib-based modules.  Everything in @files
  145. will be deleted, too (it may be empty).
  146.  
  147. The reasoning behind using this instead of just having you use the 'clean'
  148. or 'realclean' keys is that this avoids you having to remember to put Glib's
  149. stuff in your Makefile.PL's WriteMakefile arguments.
  150.  
  151. =cut
  152.  
  153. sub postamble_clean
  154. {
  155.     shift; # package name
  156. "
  157. realclean ::
  158.     -\$(RM_RF) build blib_done perl-\$(DISTNAME).spec ".join(" ", @_)."
  159. ";
  160. }
  161.  
  162. =item string = Glib::MakeHelper->postamble_docs (@xs_files)
  163.  
  164. NOTE: this is The Old Way.  see L<postamble_docs_full> for The New Way.
  165.  
  166. Create and return the text of Makefile rules to build documentation from
  167. the XS files with Glib::ParseXSDoc and Glib::GenPod.
  168.  
  169. Use this in your MY::postamble to enable autogeneration of POD.
  170.  
  171. This updates dependencies with the list of pod names generated by an earlier
  172. run of C<do_pod_files>.
  173.  
  174. There is a special Makefile variable POD_DEPENDS that should be set to the
  175. list of files that need to be created before the doc.pl step is run, include
  176. files.
  177.  
  178. There is also a variable BLIB_DONE which should be used as a dependency
  179. anywhere a rule needs to be sure that a loadable and working module resides in
  180. the blib directory before running.
  181.  
  182. =cut
  183.  
  184. sub postamble_docs
  185. {
  186.     my ($class, @xs_files) = @_;
  187.     return Glib::MakeHelper->postamble_docs_full (XS_FILES => \@xs_files);
  188. }
  189.  
  190. =item string = Glib::MakeHelper->postamble_docs_full (...)
  191.  
  192. Create and return the text of Makefile rules to build documentation from
  193. the XS files with Glib::ParseXSDoc and Glib::GenPod.
  194.  
  195. Use this in your MY::postamble to enable autogeneration of POD.
  196.  
  197. This updates dependencies with the list of pod names generated by an earlier
  198. run of C<do_pod_files>.
  199.  
  200. There is a special Makefile variable POD_DEPENDS that should be set to the
  201. list of files that need to be created before the doc.pl step is run, include
  202. files.
  203.  
  204. There is also a variable BLIB_DONE which should be used as a dependancy
  205. anywhere a rule needs to be sure that a loadable and working module resides in
  206. the blib directory before running.
  207.  
  208. The parameters are a list of key=>value pairs.  You must specify at minimum
  209. either DEPENDS or XS_FILES.
  210.  
  211. =over
  212.  
  213. =item DEPENDS => ExtUtils::Depends object
  214.  
  215. Most gtk2-perl modules use ExtUtils::Depends to find headers, typemaps,
  216. and other data from parent modules and to install this data for child
  217. modules.  We can find from this object the list of XS files to scan for
  218. documentation, doctype mappings for parent modules, and other goodies.
  219.  
  220. =item XS_FILES => \@xs_file_names
  221.  
  222. A list of xs files to scan for documentation.  Ignored if DEPENDS is
  223. used.
  224.  
  225. =item DOCTYPES => \@doctypes_file_names
  226.  
  227. List of filenames to pass to C<Glib::GenPod::add_types>.  May be omitted.
  228.  
  229. =item COPYRIGHT => string
  230.  
  231. POD text to be inserted in the 'COPYRIGHT' section of each generated page.
  232. May be omitted.
  233.  
  234. =item COPYRIGHT_FROM => file name
  235.  
  236. The name of a file containing the POD to be inserted in the 'COPYRIGHT'
  237. section of each generated page.  May be omitted.
  238.  
  239. =item NAME => extension name
  240.  
  241. The name of the extension, used to set $Glib::GenPod::MAIN_MOD (used in the
  242. generated see-also listings).  May be omitted in favor of the name held
  243. inside the ExtUtils::Depends object.  If DEPENDS is also specified, NAME wins.
  244.  
  245. =back
  246.  
  247. =cut
  248.  
  249. sub postamble_docs_full {
  250.     my $class = shift; # package name
  251.     my %params = @_;
  252.  
  253.     croak "Usage: $class\->postamble_docs_full (...)\n"
  254.         . "  where ... is a list of key/value pairs including at the\n"
  255.         . "  very least one of DEPENDS=>\$extutils_depends_object or\n"
  256.         . "  XS_FILES=>\@xs_files\n"
  257.         . "    "
  258.         unless $params{DEPENDS} or $params{XS_FILES};
  259.  
  260.     my @xs_files = ();
  261.     my @doctypes = ();
  262.     my $add_types = '';
  263.     my $copyright = '';
  264.     my $name = '';
  265.  
  266.     if ($params{DOCTYPES}) {
  267.         @doctypes = ('ARRAY' eq ref $params{DOCTYPES})
  268.                   ? @{$params{DOCTYPES}}
  269.                   : ($params{DOCTYPES});
  270.     }
  271.  
  272.     if (UNIVERSAL::isa ($params{DEPENDS}, 'ExtUtils::Depends')) {
  273.         my $dep = $params{DEPENDS};
  274.  
  275.         # fetch list of XS files from depends object.
  276.         # HACK ALERT: the older versions of ExtUtils::Depends
  277.         # (<0.2) use a different key layout and don't store enough
  278.         # metadata about the dependencies, so we require >=0.2;
  279.         # however, the older versions don't support import version
  280.         # checking (in fact they don't support version-checking at
  281.         # all), so the "use" test in a Makefile.PL can't tell if
  282.         # it has loaded a new enough version!
  283.         # the rewrite at version 0.200 added the get_dep() method,
  284.         # which we use, so let's check for that.
  285.         unless (defined &ExtUtils::Depends::get_deps) {
  286.             use ExtUtils::MakeMaker;
  287.             warn "ExtUtils::Depends is too old, need at "
  288.                . "least version 0.2";
  289.             # this is so that CPAN builds will do the upgrade
  290.             # properly.
  291.             WriteMakefile(
  292.                 PREREQ_FATAL => 1,
  293.                 PREREQ_PM => { 'ExtUtils::Depends' => 0.2, },
  294.             );
  295.             exit 1; # not reached.
  296.         }
  297.         # continue with the excessive validation...
  298.         croak "value of DEPENDS key must be an ExtUtils::Depends object"
  299.             unless UNIVERSAL::isa $dep, 'ExtUtils::Depends';
  300.         croak "corrupt or invalid ExtUtils::Depends instance -- "
  301.             . "the xs key is "
  302.             .(exists ($dep->{xs}) ? "missing" : "broken")."!"
  303.             unless exists $dep->{xs}
  304.                and 'ARRAY' eq ref $dep->{xs};
  305.  
  306.         # finally, *this* is what we wanted.
  307.         @xs_files = @{$dep->{xs}};
  308.  
  309.         # fetch doctypes files from the depends' dependencies.
  310.         my %deps = $dep->get_deps;
  311.         foreach my $d (keys %deps) {
  312.             my $f = File::Spec->catfile ($deps{$d}{instpath},
  313.                                          'doctypes');
  314.             #warn "looking for $f\n";
  315.             push @doctypes, $f
  316.                 if -f $f;
  317.         }
  318.  
  319.         # the depends object conveniently knows the main module name.
  320.         $name = $dep->{name};
  321.     } else {
  322.         @xs_files = @{ $params{XS_FILES} };
  323.     }
  324.  
  325.     if ($params{COPYRIGHT}) {
  326.         $copyright = $params{COPYRIGHT};
  327.     } elsif ($params{COPYRIGHT_FROM}) {
  328.         open IN, $params{COPYRIGHT_FROM} or
  329.             croak "can't open $params{COPYRIGHT_FROM} for reading: $!\n";
  330.         local $/ = undef;
  331.         $copyright = <IN>;
  332.         close IN;
  333.     }
  334.  
  335.     if ($copyright) {
  336.         # this text has to be escaped for both make and the shell.
  337.         $copyright =~ s/\n/\\n/gm; # collapse to one line.
  338.         $copyright =~ s/"/\"/gm;   # escape double-quotes
  339.         $copyright = "\$\$Glib::GenPod::COPYRIGHT=\"$copyright\";";
  340.     }
  341.  
  342.     # the module name specified explicitly overrides the one in a
  343.     # depends object.
  344.     $name = $params{NAME} if $params{NAME};
  345.     # now sanitize
  346.     if ($name) {
  347.         # this is supposed to be a module name; names don't have
  348.         # things in them that need escaping, so let's leave it alone.
  349.         # that way, if there's a quoting error, the user will figure
  350.         # it out real quick.
  351.         $name = "\$\$Glib::GenPod::MAIN_MOD=\"$name\";";
  352.     }
  353.  
  354.     #warn "".scalar(@doctypes)." doctype files\n";
  355.     #warn "".scalar(@xs_files)." xs files\n";
  356.     
  357.     $add_types = "add_types (".join(", ",map {"\"$_\""} @doctypes)."); "
  358.         if @doctypes;
  359.  
  360.     my $docgen_code = ''
  361.         . $add_types
  362.         . ' '
  363.         . $copyright
  364.         . ' '
  365.         . $name
  366.         . ' $(POD_SET) '
  367.         . 'xsdoc2pod("build/doc.pl", "$(INST_LIB)", "build/podindex");';
  368.  
  369.     #warn "docgen_code: $docgen_code\n";
  370.  
  371.     # BLIB_DONE should be set to something we can depend on that will
  372.     # ensure that we are safe to link against an up to date module out
  373.     # of blib. basically what we need to wait on is the static/dynamic
  374.     # lib file to be created. the following trick is intended to handle
  375.     # both of those cases without causing the other to happen.
  376.     my $blib_done;
  377.     # this is very sloppy, because different makes have different
  378.     # conditional syntaxes.
  379.     require Config;
  380.     if ($Config::Config{make} eq 'nmake') {
  381.         $blib_done = "
  382. !If \"\$(LINKTYPE)\" == \"dynamic\"
  383. BLIB_DONE=\$(INST_DYNAMIC)
  384. !ELSE
  385. BLIB_DONE=\$(INST_STATIC)
  386. !ENDIF
  387. ";
  388.     } else {
  389.         # assuming GNU Make
  390.         $blib_done = "
  391. ifeq (\$(LINKTYPE), dynamic)
  392.     BLIB_DONE=\$(INST_DYNAMIC)
  393. else
  394.     BLIB_DONE=\$(INST_STATIC)
  395. endif
  396. ";
  397.     }
  398.  
  399. "
  400. BLIB_DONE=
  401. $blib_done
  402.  
  403. # documentation stuff
  404. build/doc.pl :: Makefile @xs_files
  405.     $^X -I \$(INST_LIB) -I \$(INST_ARCHLIB) -MGlib::ParseXSDoc \\
  406.         -e 'xsdocparse (qw(@xs_files))' > \$@
  407.  
  408. # passing all of these files through the single podindex file, which is 
  409. # created at the same time, prevents problems with -j4 where xsdoc2pod would 
  410. # have multiple instances
  411. @gend_pods :: build/podindex \$(POD_DEPENDS)
  412.  
  413. build/podindex :: \$(BLIB_DONE) Makefile build/doc.pl
  414.     $^X -I \$(INST_LIB) -I \$(INST_ARCHLIB) -MGlib::GenPod -M\$(NAME) \\
  415.         -e '$docgen_code'
  416.  
  417. \$(INST_LIB)/\$(FULLEXT)/:
  418.     $^X -MExtUtils::Command -e mkpath \$@
  419.  
  420. \$(INST_LIB)/\$(FULLEXT)/index.pod :: \$(INST_LIB)/\$(FULLEXT)/ build/podindex
  421.     $^X -e 'print \"\\n=head1 NAME\\n\\n\$(NAME) API Reference Pod Index\\n\\n=head1 PAGES\\n\\n=over\\n\\n\"' \\
  422.         > \$(INST_LIB)/\$(FULLEXT)/index.pod
  423.     $^X -nae 'print \"=item L<\$\$F[1]>\\n\\n\";' < build/podindex >> \$(INST_LIB)/\$(FULLEXT)/index.pod
  424.     $^X -e 'print \"=back\\n\\n\";' >> \$(INST_LIB)/\$(FULLEXT)/index.pod
  425. "
  426. }
  427.  
  428. =item string = Glib::MakeHelper->postamble_rpms (HASH)
  429.  
  430. Create and return the text of Makefile rules to manage building RPMs.
  431. You'd put this in your Makefile.PL's MY::postamble.
  432.  
  433. I<HASH> is a set of search and replace keys for the spec file.  All 
  434. occurences of @I<key>@ in the spec file template perl-$(DISTNAME).spec.in
  435. will be replaced with I<value>.  'VERSION' and 'SOURCE' are supplied for
  436. you.  For example:
  437.  
  438.  Glib::MakeHelper->postamble_rpms (
  439.         MYLIB     => 2.0.0, # we can work with anything from this up
  440.         MYLIB_RUN => 2.3.1, # we are actually compiled against this one
  441.         PERL_GLIB => 1.01,  # you must have this version of Glib
  442.  );
  443.  
  444. will replace @MYLIB@, @MYLIB_RUN@, and @PERL_GLIB@ in spec file.  See
  445. the build setups for Glib and Gtk2 for examples.
  446.  
  447. Note: This function just returns an empty string on Win32.
  448.  
  449. =cut
  450.  
  451. sub postamble_rpms
  452. {
  453.     shift; # package name
  454.  
  455.     return '' if $^O eq 'MSWin32';
  456.     
  457.     my @dirs = qw{$(RPMS_DIR) $(RPMS_DIR)/BUILD $(RPMS_DIR)/RPMS 
  458.               $(RPMS_DIR)/SOURCES $(RPMS_DIR)/SPECS $(RPMS_DIR)/SRPMS};
  459.     my $cwd = getcwd();
  460.     
  461.     chomp (my $date = `date +"%a %b %d %Y"`);
  462.  
  463.     my %subs = (
  464.         'VERSION' => '$(VERSION)',
  465.         'SOURCE'  => '$(DISTNAME)-$(VERSION).tar.gz',
  466.         'DATE'    => $date,
  467.         @_,
  468.     );
  469.     
  470.     my $substitute = '$(PERL) -npe \''.join('; ', map {
  471.             "s/\\\@$_\\\@/$subs{$_}/g";
  472.         } keys %subs).'\'';
  473.  
  474. "
  475.  
  476. RPMS_DIR=\$(HOME)/rpms
  477.  
  478. \$(RPMS_DIR)/:
  479.     -mkdir @dirs
  480.  
  481. SUBSTITUTE=$substitute
  482.  
  483. perl-\$(DISTNAME).spec :: perl-\$(DISTNAME).spec.in \$(VERSION_FROM) Makefile
  484.     \$(SUBSTITUTE) \$< > \$@
  485.  
  486. dist-rpms :: Makefile dist perl-\$(DISTNAME).spec \$(RPMS_DIR)/
  487.     cp \$(DISTNAME)-\$(VERSION).tar.gz \$(RPMS_DIR)/SOURCES/
  488.     rpmbuild -ba --define \"_topdir \$(RPMS_DIR)\" perl-\$(DISTNAME).spec
  489.  
  490. dist-srpms :: Makefile dist perl-\$(DISTNAME).spec \$(RPMS_DIR)/
  491.     cp \$(DISTNAME)-\$(VERSION).tar.gz \$(RPMS_DIR)/SOURCES/
  492.     rpmbuild -bs --nodeps --define \"_topdir \$(RPMS_DIR)\" perl-\$(DISTNAME).spec
  493. ";
  494. }
  495.  
  496. package MY;
  497.  
  498. =back
  499.  
  500. =head1 NOTICE
  501.  
  502. The MakeMaker distributed with perl 5.8.x generates makefiles with a bug that
  503. causes object files to be created in the wrong directory.  There is an override
  504. inserted by this module under the name MY::const_cccmd to fix this issue.
  505.  
  506. =cut
  507.  
  508. sub const_cccmd {
  509.     my $inherited = shift->SUPER::const_cccmd (@_);
  510.     return '' unless $inherited;
  511.     require Config;
  512.     # a more sophisticated match may be necessary, but this works for me.
  513.     if ($Config::Config{cc} eq "cl") {
  514.         $inherited .= ' /Fo$@';
  515.     } else {
  516.         $inherited .= ' -o $@';
  517.     }
  518.     $inherited;
  519. }
  520.  
  521. 1;
  522.  
  523. =head1 AUTHOR
  524.  
  525. Ross McFarland E<lt>rwmcfa1 at neces dot comE<gt>
  526.  
  527. hacked up and documented by muppet.
  528.  
  529. =head1 COPYRIGHT AND LICENSE
  530.  
  531. Copyright 2003-2004 by the gtk2-perl team
  532.  
  533. This library is free software; you can redistribute it and/or modify
  534. it under the terms of the Lesser General Public License (LGPL).  For 
  535. more information, see http://www.fsf.org/licenses/lgpl.txt
  536.  
  537. =cut
  538.